/*
** Copyright (c) 2008 Mobile GCalendarManager
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.ui.cli;

import java.io.BufferedReader;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

/**
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 *
 */
public enum ConsoleIO
{
  Console

  {
    @Override
    public String       password(final String format,
                                 final Object ... args)
    {
      String            pass  = null;

      while (pass == null)
      {
        pass = new String(System.console().readPassword(format, args));
        lines++;
      }
      return (pass);
    }

    @Override
    public String       input(final String format, final Object ... args)
    {
      String            key = null;

      while (key == null)
      {
        try
        {
          key = System.console().readLine(format, args);
        }
        catch (final IOError e)
        {
          logger.error(e.getMessage(), e);
          key = "";
        }
        lines++;
      }
      return (key);
    }

    @Override
    public void         output(final String format,
                               final Object ... args)
    {
      if (format != null)
      {
        System.console().format(format, args);
      }
    }

  },
  Streams
  {
    @Override
    public String       password(final String format,
                                 final Object ... args)
    {
      String            pass  = null;

      while (pass == null)
      {
        pass = new String(input(format, args));
        lines++;
      }
      return (pass);
    }

    @Override
    public String       input(final String format,
                                    final Object ... args)
    {
      String                  key = null;
      final InputStreamReader isr = new InputStreamReader(System.in);
      final BufferedReader    br  = new BufferedReader(isr);

      while (key == null)
      {
        output(format, args);
        try
        {
          key = br.readLine();
        }
        catch (final IOException e)
        {
          logger.error(e.getMessage(), e);
          key = "";
        }
        lines++;
      }
      return (key);
    }

    @Override
    public void         output(final String format, final Object ... args)
    {
      if (format != null)
      {
        System.out.format(format, args);
      }
    }
  };

  private static final Logger   logger  = Logger.getLogger(ConsoleIO.class);
  private static int            lines   = 0;


  public abstract String password(final String format, final Object ... args);

  public abstract String input(final String format, final Object ... args);

  public abstract void   output(final String format, final Object ... args);

  public void            pause()
  {
    pause("");
  }

  public void            pause(final String format, final Object ... args)
  {
    input(format, args);
  }

  public int  getLines()
  {
    return (lines);
  }
}
